<%--
  Created by IntelliJ IDEA.
  User: amin
  Date: 2016/5/12
  Time: 21:55
  To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" pageEncoding="UTF-8"%>
<%
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath();
%>
<script type="text/javascript">
 

    $(function(){
        var dx = 0, dy = 0;
        var initialScale = 1;
        var currentScale = 1;

        var target = $("#img_show_big");
        //target.style.webkitTransition = "all ease 0.05s";
        touch.on('#img_show_big', 'touchstart', function (ev) {
            ev.preventDefault();
        });
        touch.on('#img_show_big', 'pinchend', function (ev) {
            currentScale = ev.scale - 1;
            currentScale = initialScale + currentScale;
            currentScale = currentScale > 4 ? 4 : currentScale;
            currentScale = currentScale < 0.5 ? 0.5 : currentScale;
            this.style.webkitTransform = 'scale(' + currentScale + ')';
        });
        touch.on('#img_show_big', 'pinchend', function (ev) {
            initialScale = currentScale;
        });


//            touch.on('#img_show_big', 'touchstart', function(ev){
//                ev.preventDefault();
//            });
        touch.on('#img_show_big', 'drag', function (ev) {
            dx = dx || 0;
            dy = dy || 0;
            var offx = dx + ev.x + "px";  
            var offy = dy + ev.y + "px";
            this.style.webkitTransform = 'translate3d(' + offx + ',' + offy + ',0)' + 'scale(' + currentScale + ')'
        });
        touch.on('#img_show_big', 'dragend', function (ev) {
            dx += ev.x;
            dy += ev.y;
        });

        $('#imageContainer').height($('#page-previewImage').height() - 100);

    });

    $(function(){

        $("[data-role='navbar']").navbar();
        //$("[data-role='header'], [data-role='footer']").toolbar();

        var urls = "";
        $(".imgslist[ref='show']").each(function(){
            urls += $(this).attr("src") + ",";
        });
        urls = urls.substring(0,urls.length-1);
        $("#divUrls").text(urls);
        $("#imageTotal").text(urls.split(",").length);

    });

    wx.config({
        debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
        appId: '<s:property value="jsapiSignature.appId" />', // 必填，公众号的唯一标识
        timestamp: '<s:property value="jsapiSignature.timestamp" />', // 必填，生成签名的时间戳
        nonceStr: '<s:property value="jsapiSignature.noncestr" />', // 必填，生成签名的随机串
        signature: '<s:property value="jsapiSignature.signature" />',// 必填，签名，见附录1
        jsApiList: ['checkJsApi','chooseImage', 'previewImage', 'uploadImage', 'downloadImage'] // 必填，需要使用的JS接口列表，所有JS接口列表见附录2
    });

    wx.ready(function () {
        var u = navigator.userAgent, app = navigator.appVersion;
        var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Linux') > -1; //android终端或者uc浏览器
//            var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
//            alert('是否是Android：'+isAndroid);
//            alert('是否是iOS：'+isiOS);

        $("#prevImage").on("click", function () {

            var imageIndex = parseInt($("#imageIndex").text()) - 1;
            if (imageIndex < 1) {
                alert("已经是第一张了！");
                return;
            }
            var img = $("img[data-index=" + imageIndex + "]").first();
            //$("#imageContainer").html($(img).clone());
            $("#img_show_big").attr("src", $(img).attr("src"));
            $("#imageIndex").text("" + imageIndex);
        });

        $("#nextImage").on("click", function () {

            var imageIndex = parseInt($("#imageIndex").text()) + 1;
            var imageTotal = $("#imageTotal").text();
            if (imageIndex > imageTotal) {
                alert("已经是最后一张了！");
                return;
            }
            var img = $("img[data-index=" + imageIndex + "]").first();
            //$("#imageContainer").html($(img).clone());
            $("#img_show_big").attr("src", $(img).attr("src"));
            $("#imageIndex").text("" + imageIndex);
        });

        $(".imgslist").click(function () {
            if (isAndroid) {
//                    alert("imageIndex:"+$(this).attr("data-index")+" imageTotal:"+$("#divUrls").text().split(",").length);
                $("#imageIndex").text($(this).attr("data-index"));
                $("#imageTotal").text($("#divUrls").text().split(",").length);
                //$("#imageContainer").html($(this).clone());
                $("#img_show_big").attr("src", $(this).attr("src"));
                $(':mobile-pagecontainer').pagecontainer('change', '#page-previewImage', {
                    changeHash: false,
                    reverse: true,
                    transition: 'slidedown'
                });
            } else {
                var url = $(this).attr("src");
                var arr = $("#divUrls").text().split(",");
                wx.previewImage({
                    current: url,
                    urls: arr
                });
            }
        });

    });

    wx.error(function (res) {
        //alert(res.errMsg);
    });

    function signclick(id,targetClass,entityKey){
        $.mobile.loading( "show", {
            text: "正在获取地理位置，请稍后...",
            textVisible: true,
            theme: "b",
            textonly: false,
            html: ""
        });
        getCheckInfo(id,targetClass,entityKey);
    }

    function getCheckInfo(id,targetClass,entityKey){
        wx.ready(function(){
            wx.getLocation({
                success: function (res) {
                    hide();
                    //var data = {keyId:id,latitude:res.latitude,longitude:res.longitude};
                    submitCheckInfo(id,targetClass,entityKey,res.latitude,res.longitude)
                },
                fail : function(){
                    hide();
                    alert("获取位置失败，请确认是否开启GPS定位功能!");
                }
            });
        });
    }
    function submitCheckInfo(id,targetClass,entityKey,latitude,longitude){
        $.mobile.loading( "show", {
            text: "正在保存，请稍后...",
            textVisible: true,
            theme: "b",
            textonly: false,
            html: ""
        });
        $.ajax({
            type: "POST",
            url: "ajax!assign.action",
            cache:false,dataType:"json",
            data:{keyId:id,targetClass:targetClass,latitude:latitude,longitude:longitude},
            async:true,
            success: function(data){
                hide();
                if(data.result.errorCode == 404 || data.result.errorCode == "404"){
                    alert(data.result.errorMessage);
                }else{
                    alert(data.result.errorMessage);
                    if(entityKey=="section"){
                        if($("#surveyurl").val()!=""){
                            location.replace($("#surveyurl").val());
                        }else {
                            location.replace("ajax!section.action");
                        }
                    }else if(entityKey=="term"){
                        if($("#surveyurl").val()!=""){
                            location.replace($("#surveyurl").val());
                        }else{
                            location.replace("ajax!term.action");
                        }
                    }else if(entityKey=="agentrain"){
                        if($("#surveyurl").val()!=""){
                            location.replace($("#surveyurl").val());
                        }else {
                            location.replace("ajax!agenttrain.action");
                        }
                    }else if(entityKey=="business"){
                        if($("#surveyurl").val()!=""){
                            location.replace($("#surveyurl").val());
                        }else {
                            location.replace("ajax!business.action");
                        }
                    } else if(entityKey=="train"){
                        if($("#surveyurl").val()!=""){
                            location.replace($("#surveyurl").val());
                        }else {
                            location.replace("ajax!train.action");
                        }
                    }else if(entityKey=="management"){
                        if($("#surveyurl").val()!=""){
                            location.replace($("#surveyurl").val());
                        }else {
                            location.replace("ajax!management.action");
                        }
                    }else if(entityKey=="innertrain"){
                        if($("#surveyurl").val()!=""){
                            location.replace($("#surveyurl").val());
                        }else {
                            location.replace("ajax!innertrain.action");
                        }
                    }
                }
            },
            error: function(){
                hide();
            }
        });
    }

</script>
